/**
 * 
 */
package ch.bfh.mobicomp.ui.in;

import ch.bfh.mobicomp.AlarmManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;

public class AlarmSMS extends BroadcastReceiver {
	
	private SharedPreferences pref;	
	
	private static final String tag="BroadcastReceiver";
	private static final String SMS_COMMAND_ALARM = "alarm";
	private static final String SMS_COMMAND_CONFIG = "config";
	
	private static final String SMS_OPTION_ENABLE = "enable";
	private static final String SMS_OPTION_DISABLE = "disable";
	private static final String SMS_OPTION_LIGHT = "light";
	private static final String SMS_OPTION_ACCELERATION = "acceleration";
	private static final String SMS_OPTION_COMPASS = "compass";
	private static final String SMS_OPTION_SMS_STATE = "sms";
	private static final String SMS_OPTION_SMS_NUMBER = "smsnumber";
	private static final String SMS_OPTION_EMAIL_STATE = "email";
	private static final String SMS_OPTION_EMAIL_ADDRESS = "emailaddress";
	
//	public AlarmSMS(Context context) {
//		this.pref = context.getSharedPreferences(AlarmActivity.SP_NAME, Context.MODE_PRIVATE);
//	}
	
	
	@Override
	 public void onReceive(Context context, Intent intent) {
		this.pref = context.getSharedPreferences(AlarmActivity.SP_NAME,Context.MODE_PRIVATE);
		Log.d(tag, "Got SMS");
    	Bundle bundle = intent.getExtras();
    	if (bundle == null)
    		return;
    	Object[] pdus = (Object[]) bundle.get("pdus");
    	for (Object pdu : pdus) {
    		{
    			if (pdu == null)
    				continue;
    			
    			String msg = SmsMessage.createFromPdu((byte[]) pdu).getMessageBody();
    			if (msg == null)
    				continue;
    			
    			msg = msg.toLowerCase();
    			Log.d(tag, "SMS Text: " + msg);
    			String[] sms = msg.split(" ");
    			
    			if(sms[0].equals(SMS_COMMAND_ALARM)) {
    				commandAlarm(context, sms);
    				Log.d(tag, "Aborting broadcast");
    				abortBroadcast();
    			} else if(sms[0].equals(SMS_COMMAND_CONFIG)) {
    				commandConfig(context, sms);
    				Log.d(tag, "Aborting broadcast");
    				abortBroadcast();
    			} else {
    				Log.d(tag, "Command invalid");
    			}
    		}

    	}
    }
	
	private void commandAlarm(Context context, String[] sms) {
		boolean changed = false;
		if(sms[1].equals(SMS_OPTION_ENABLE)) {
			Log.d(tag, "Enable alarmService");
			Editor editor = pref.edit();
			editor.putBoolean(AlarmActivity.SP_KEY_SERVICE_STATE, true);
			editor.commit();
			changed = true;
		} else if(sms[1].equals(SMS_OPTION_DISABLE)) {
			Log.d(tag, "Disable alarmService");
			Editor editor = pref.edit();
			editor.putBoolean(AlarmActivity.SP_KEY_SERVICE_STATE, false);
			editor.commit();
			changed = true;
		} else {
			Log.d(tag, "Option invalid");
		}
		
		if(changed) {
			Log.d(tag, "Config changed");
			Intent broadcastIntent = new Intent(AlarmManager.INTENT_ALARM_CONFIG_CHANGED);
			context.sendBroadcast(broadcastIntent);
		}
	}
	
	private void commandConfig(Context context, String[] sms) {
		boolean changed = false;
		
		if(sms[1].equals(SMS_OPTION_LIGHT)) {
			if(sms[2].equals(SMS_OPTION_ENABLE)) {
				Log.d(tag, "Enable light sensor");
				Editor editor = pref.edit();
				editor.putBoolean(AlarmActivity.SP_KEY_ALARM_LIGHT, true);
				editor.commit();
				changed = true;
			} else if(sms[2].equals(SMS_OPTION_DISABLE)) {
				Log.d(tag, "Disable light sensor");
				Editor editor = pref.edit();
				editor.putBoolean(AlarmActivity.SP_KEY_ALARM_LIGHT, false);
				editor.commit();
				changed = true;
			} else {
				Log.d(tag, "Suboption invalid");
			}
		} else if(sms[1].equals(SMS_OPTION_ACCELERATION)) {
		} else if(sms[1].equals(SMS_OPTION_COMPASS)) {
		} else if(sms[1].equals(SMS_OPTION_SMS_STATE)) {
		} else if(sms[1].equals(SMS_OPTION_SMS_NUMBER)) {
		} else if(sms[1].equals(SMS_OPTION_EMAIL_STATE)) {
		} else if(sms[1].equals(SMS_OPTION_EMAIL_ADDRESS)) {
		} else {
			Log.d(tag,"Option invalid");
		}
		
		if(changed) {
			Log.d(tag, "Config changed");
			Intent broadcastIntent = new Intent(AlarmManager.INTENT_ALARM_CONFIG_CHANGED);
			context.sendBroadcast(broadcastIntent);
		}
	}

}
